package com.mooc.apigateway.dao;

import com.mooc.apigateway.common.RestResponse;
import com.mooc.apigateway.config.GenericRest;
import com.mooc.apigateway.model.Community;
import com.mooc.apigateway.utils.Rests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class CommunityDao {
    @Autowired
    private GenericRest rest;
    @Value("${house.service.name}")
    private String houseServiceName;

    public List<Community> getAllCommunity() {

        return Rests.exc(() ->{
            String url = Rests.toUrl(houseServiceName, "/house/getAllCommunity");
            ResponseEntity<RestResponse<List<Community>>> responseEntity =
                    rest.get(url, new ParameterizedTypeReference<RestResponse<List<Community>>>() { });
            return responseEntity.getBody();
        }).getResult();
    }


}
